package com.ruoyi.common.utils.img;

import com.ruoyi.common.config.RuoYiConfig;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtilsTest {

    /**
     * 默认上传的地址
     */
    private static String defaultBaseDir = RuoYiConfig.getProfile();

    public static String savePic(File stuPic, File zhangPic,String name,String job, String title,String type) {
        try {
            // 图片格式
            String format = "png";
            String filePath =  defaultBaseDir + "/zs." + format;
            // 加载模板图片和需要替换的图片数据
            BufferedImage stu = ImageIO.read(stuPic);
            BufferedImage zhang =  ImageIO.read(zhangPic);
            BufferedImage beijing =  ImageIO.read(new File("D:\\backgroundTemplate.jpg"));
            // 创建一个新的空白图片
            BufferedImage newImage = new BufferedImage(8000, 10000, BufferedImage.TYPE_INT_ARGB);
            newImage.setRGB(255,255,255);
            // 使用Java 2D API或JAI在空白图片上绘制模板图片
            Graphics2D graphics = newImage.createGraphics();
            graphics.setBackground(Color.WHITE);
            graphics.fillRect(0,0,newImage.getWidth(), newImage.getHeight());
            //添加背景图片
            graphics.drawImage(beijing, 0, 0,8000, 10000, null);
            Font font = new Font("微软雅黑", Font.BOLD, 500);
            graphics.setFont(font);
            graphics.setColor(Color.WHITE);
            //标题
            FontMetrics fontMetrics = graphics.getFontMetrics(font);
            int textWidth = fontMetrics.stringWidth(title);
            int centerX = 8000 / 2 - textWidth / 2;
            graphics.drawString(title, centerX, 1000);
            //证书类型
            Font certTypeFont = new Font("微软雅黑", Font.BOLD, 400);
            graphics.setFont(certTypeFont);
            int textWidth2 = fontMetrics.stringWidth(type);
            int centerX2 = 8000 / 2 - textWidth2 / 2;
            graphics.drawString(type, centerX2, 2000);
            //学生照片
            int centerXS = 8000 / 2 - 3500 / 2;
            graphics.drawImage(stu, centerXS, 2500,3500, 4000, null);
            //姓名及工作单位 及 颜色
            graphics.setColor(Color.decode("#7ac5be"));
            graphics.setFont(new Font("微软雅黑", Font.BOLD, 350));
            graphics.drawString("姓名："+name, 2500, 7200);
            graphics.drawString("工作单位："+job, 2500, 8000);
            graphics.setColor(Color.WHITE);
            //尾部说明
            Font font2 = new Font("微软雅黑", Font.BOLD, 500);
            graphics.setFont(font2);
            int textWidth3 = fontMetrics.stringWidth("浦东新区妇幼保健中心制");
            int centerX3 = 8000 / 2 - textWidth3 / 2;
            graphics.drawString("浦东新区妇幼保健中心制", centerX3, 9500);
            //机构公章
            int centerXZ = 8000 / 2 - 5500 / 2;
            graphics.drawImage(zhang, centerXZ, 5500, 5500, 4500, null);
            // 保存生成的图片
            ImageIO.write(newImage, format, new File(filePath));
            return filePath;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static int getMinHeight(int srcImgHeight1, int srcImgHeight2) {
        if (srcImgHeight1 > srcImgHeight2) {
            return srcImgHeight1;
        }
        return srcImgHeight2;
    }

}